package cn.liumouren.jzxd.service.impl;

import cn.liumouren.jzxd.common.utils.PageUtils;
import cn.liumouren.jzxd.common.utils.Query;
import cn.liumouren.jzxd.dao.PictureDao;
import cn.liumouren.jzxd.entity.Picture;
import cn.liumouren.jzxd.service.PictureService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("pictureService")
public class PictureServiceImpl extends ServiceImpl<PictureDao, Picture> implements PictureService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Picture> page = this.page(
                new Query<Picture>().getPage(params),
                new QueryWrapper<Picture>()
        );

        return new PageUtils(page);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String savePic(Picture entity) {
        String idStr = IdWorker.getIdStr();
        entity.setId(idStr);
        entity.setAddTime(new Date());
        int i = baseMapper.insert(entity);
        return i == 1 ? idStr : null;
    }

    @Override
    public List<Picture> getCarousels() {
        List<Picture> pictures = baseMapper.selectList(new QueryWrapper<Picture>().lambda()
                .eq(Picture::getCarousel, 1));
        return pictures;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(Picture entity) {
        return SqlHelper.retBool(baseMapper.updateById(entity));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return false;
        }
        return SqlHelper.retBool(baseMapper.deleteBatchIds(idList));
    }

}